import cgi
import sys, os
import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import facebook
from facebook import FacebookError
from google.appengine.ext import db

class Store(db.Model):
    id = db.IntegerProperty(required=True)
    name = db.StringProperty(required=True)
    link=db.LinkProperty(required=True)#store image link
    bonusPoints = db.IntegerProperty(required=True,default=100)#fb bid money
    reliable = db.RatingProperty(required=True,default=80)
    joinedDate= db.DateTimeProperty(auto_now=True)

#Model User File begin 
class FbUserProfile(db.Model):#belong to store
    store = db.ReferenceProperty(Store,collection_name='users')#mabe someone need to open more store, but wait for argue
    uid = db.IntegerProperty(required=True)
    bonusPoints = db.IntegerProperty(required=True,default=100)#fb bid money
    reliable = db.RatingProperty(required=True,default=80)
    joinedDate= db.DateTimeProperty(auto_now=True)

class IM(db.Model):#also can use in store
    user = db.ReferenceProperty(db.Model,collection_name='msn')    
    type = db.StringProperty(required=True)
    addr = db.IMProperty(required=True)

class Email(db.Model):#also can use in store
    user = db.ReferenceProperty(db.Model,collection_name='emails')    
    type = db.StringProperty(required=True)
    addr = db.EmailProperty(required=True)
    
class Phone(db.Model):#also can use in store
    user = db.ReferenceProperty(db.Model,collection_name='phones')
    type = db.StringProperty(required=True)    
    tel = db.PhoneNumberProperty(required=True)
  
class Address(db.Model):#also can use in store
    user = db.ReferenceProperty(db.Model,collection_name='postalAddress')
    type = db.StringProperty(required=True)    
    addr = db.PostalAddressProperty(required=True)
    
class OtherNote(db.Model):#also can use in store
    user = db.ReferenceProperty(db.Model,collection_name='others')    
    title = db.StringProperty(required=True)
    content = db.StringProperty(required=True)

#Model User File end
#Model Product File begin
class Product(db.Model):#belong to store
    owner = db.ReferenceProperty(Store,collection_name='products')
    name = db.StringProperty(required=True)
    des = db.TextProperty(default=u"about...")
    status = db.IntegerProperty(required=True)
    categories = db.ListProperty(db.Key)
    images = db.ListProperty(db.Key)    
    created = db.DateTimeProperty(auto_now=True)

class Price(db.Model):
    Product = db.ReferenceProperty(Product,collection_name='price')
    PurchasePrice=db.IntegerProperty(required=True)#in the begining just direct
    
class Trade(db.Model):
    Product = db.ReferenceProperty(Product,collection_name='trade')
    tradeUserId = db.IntegerProperty(required=True)#mr, trade
    ways = db.StringProperty()
    charge =db.IntegerProperty(required=True)
    addr = db.PostalAddressProperty()
    
class FinalPrice(db.Model):
    Product = db.ReferenceProperty(Product,collection_name='final')
    price = db.IntegerProperty(required=True)#in the begining just direct

class SeedCapital(db.Model):
    Product = db.ReferenceProperty(Product,collection_name='seedCapital')
    price = db.IntegerProperty(required=True)#in the begining just direct
  
  
class Category(db.Model):
    name = db.StringProperty(required=True)
    description = db.StringProperty(multiline=True)

    @property
    def products(self):
        return Product.gql('WHERE categories = :1',self.key())
#Model Product File end
#Model Image File begin
class Image(db.Model):#share Image
    link=db.LinkProperty(required=True)
    
    @property
    def products(self):
        return Product.gql('WHERE images = :1',self.key())    

#Model Image File end    
